﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;

namespace QPEngine
{
    public class ConfigerHelper
    {
        /// <summary>
        /// 保存json配置文件
        /// </summary>
        private static void SaveJsonCofiger(string json, string path)
        {
            string fileName = Path.GetFileName(path);
            if (AppConst.FileNameEncryption)
            {
                fileName = MD5Helper.EncryptDES(fileName.RemoveSuffix());
                fileName = fileName +".txt";
            }
            if (File.Exists(path)) { File.Delete(path); }
            File.AppendAllText(path, json);
            Log.Debug(string.Format("保存{0}配置完成", fileName));
        }

        /// <summary>
        /// 保存CSConfiger
        /// </summary>
        public static void SaveCSConfiger()
        {
            string path = PathHelp.AppHotfixResPath + AppConst.CSConfigerName;
            string json = JsonMapper.ToJson(AppConst.csConfiger);
            SaveJsonCofiger(json, path);
        }
        /// <summary>
        /// 保存updateConfiger
        /// </summary>
        public static void SaveUpdateConfiger()
        {
            string path = PathHelp.AppHotfixResPath + AppConst.UpdateConfigerName;
            string json = JsonMapper.ToJson(AppConst.updateConfiger);
            SaveJsonCofiger(json, path);

        }
        /// <summary>
        /// 保存valueConfiger
        /// </summary>
        public static void SaveValueConfiger()
        {
            string path = PathHelp.AppHotfixResPath + AppConst.ValueConfigerName;
            string json = JsonMapper.ToJson(AppConst.valueConfiger);
            SaveJsonCofiger(json, path);

        }
        /// <summary>
        /// 保存appInfoConfiger
        /// </summary>
        public static void SaveAppInfoConfiger()
        {
            string path = PathHelp.AppHotfixResPath + AppConst.AppInfoConfigerName;
            string json = JsonMapper.ToJson(AppConst.appInfoConfiger);
            SaveJsonCofiger(json, path);
        }


        /// <summary>
        /// 保存appInstallConfiger
        /// </summary>
        public static void SaveAppInstallConfigerr()
        {
            string path = PathHelp.AppHotfixResPath + AppConst.AppInstallConfigerName;
            string json = JsonMapper.ToJson(AppConst.appInstallConfiger);
            SaveJsonCofiger(json, path);
        }
    }
}
